﻿using System;
using System.Linq;
using Eneta.Portal.Common.Definitions;
using Eneta.Portal.Common.Repository;
using Microsoft.SharePoint;

namespace Eneta.Portal.Common.Dto
{
    public class LiteratureCommentDto
    {
        public string Comment { get; set; }
        public string List { get; set; }
        public string WebUrl { get; set; }
        public string PosterName { get; set; }
        public int PosterId { get; set; }
        public int LiteratureItemId { get; set; }
        public int PosterProfileId { get; set; }
        public DateTime Created { get; set; }

        public LiteratureCommentDto(SPWeb web, SPListItem item)
        {
            LiteratureItemId = int.Parse(item[LiteratureCommentFields.ItemId].ToString());

            var posterUser = new SPFieldUserValue(web.Site.RootWeb, item[Constants.Fields.RealAuthor.Name] as string);
            PosterId = posterUser.User.ID;
            PosterName = posterUser.User.Name;

            var rep = new UserProfileRepository(web);
            var profile = rep.GetUserProfileByProfileName(posterUser.User.Name);
            if(profile != null)
                PosterProfileId = profile.Id;

            var listInfo = (item[LiteratureCommentFields.List] as string).Split(';');
            WebUrl = listInfo.First();
            List = listInfo[1];

            Comment = item[LiteratureCommentFields.Comment] as string;
            Created = (DateTime)item[SPFields.Created];
        }
    }
}
